const app = getApp()

Page({
  data: {
    technicianInfo: null,
    orderCounts: {
      pending: 0,
      processing: 0,
      completed: 0,
      review: 0
    }
  },

  onShow() {
    this.loadTechnicianInfo();
    this.loadOrderCounts();
  },

  loadTechnicianInfo() {
    const technicianInfo = wx.getStorageSync('technicianInfo');
    if (technicianInfo) {
      this.setData({ technicianInfo });
    } else {
      app.redirectToTechnicianLogin();
    }
  },

  async loadOrderCounts() {
    const technicianInfo = this.data.technicianInfo;
    if (!technicianInfo || !technicianInfo.id) {
      return;
    }
    try {
      const res = await app.request({
        url: '/api/orders',
        method: 'GET'
      });

      const allOrders = res.data.filter(order => order.technicianId === technicianInfo.id);

      const pending = allOrders.filter(order => order.status === 'CONFIRMED' || order.status === 'PENDING').length;
      const processing = allOrders.filter(order => order.status === 'PROCESSING').length;
      const completed = allOrders.filter(order => order.status === 'COMPLETED').length;
      const review = allOrders.filter(order => order.status === 'COMPLETED' && !order.review).length; // 假设未评价的已完成订单

      this.setData({
        'orderCounts.pending': pending,
        'orderCounts.processing': processing,
        'orderCounts.completed': completed,
        'orderCounts.review': review
      });

    } catch (error) {
      console.error('加载订单数量失败：', error);
    }
  },

  goToOrderList(e) {
    const status = e.currentTarget.dataset.status;
    wx.navigateTo({
      url: `/pages/worker/orderList/orderList?status=${status}`
    });
  },

  goToMyOrders() {
    wx.navigateTo({
      url: '/pages/worker/orderList/orderList' // 查看所有订单
    });
  },

  goToMyReviews() {
    wx.navigateTo({
      url: '/pages/worker/myReviews/myReviews' // 待实现
    });
  },

  goToSpecialty() {
    wx.navigateTo({
      url: '/pages/worker/specialty/specialty' // 需要新建该页面
    });
  },

  goToHelp() {
    wx.navigateTo({
      url: '/pages/worker/help/help' // 待实现
    });
  },

  goToAbout() {
    wx.navigateTo({
      url: '/pages/worker/about/about' // 待实现
    });
  },

  logout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('technicianToken');
          wx.removeStorageSync('technicianInfo');
          app.globalData.technicianInfo = null;
          app.redirectToTechnicianLogin();
        }
      }
    });
  }
}); 